package com.hyz.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hyz.pojo.IsComplete;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.asset.Asset;
import com.hyz.pojo.asset.AssetClasses;
import com.hyz.pojo.asset.AssetStatus;
import com.hyz.pojo.assetAllocation.AssetAllocation;
import com.hyz.pojo.assetImport.AssetImport;
import com.hyz.pojo.assetImport.AssetImportResult;
import com.hyz.pojo.assetMaintenance.AssetMaintenance;
import com.hyz.pojo.assetMaintenance.AssetMaintenanceResult;
import com.hyz.pojo.assetMaintenance.AssetMaintenanceStatus;
import com.hyz.pojo.assetOutbound.AssetOutbound;
import com.hyz.pojo.assetOutbound.AssetOutboundResult;
import com.hyz.pojo.assetRetirement.AssetRetirement;
import com.hyz.pojo.assetRetirement.AssetRetirementResult;
import com.hyz.pojo.assetRetirement.AssetRetirementStatus;
import com.hyz.pojo.assetSecondment.AssetSecondment;
import com.hyz.pojo.assetSecondment.AssetSecondmentResult;
import com.hyz.pojo.repository.Repository;
import com.hyz.pojo.repositoryAsset.RepositoryAsset;
import com.hyz.service.*;
import com.hyz.utils.JudgeUtils;
import com.hyz.utils.SqlDate;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author HGD
 * @date 2022/10/7 23:35
 */
@RestController
@RequestMapping("/assetManager")
public class AssetManagerController {
    @Autowired
    private AssetService assetService;
    @Autowired
    private RepositoryAssetService repositoryAssetService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private AssetImportService assetImportService;
    @Autowired
    private AssetSecondmentService asService;
    @Autowired
    private AssetOutboundService aoService;
    @Autowired
    private RepositoryAssetService raService;
    @Autowired
    private AssetAllocationService aaService;
    @Autowired
    private AssetMaintenanceService amService;
    @Autowired
    private AssetRetirementService arService;

    //-----------------------------------------------------------------
    //未分库资产批量导入
    @PostMapping("/getExcelData")
    public ResponseResult<Object> getExcelAsset(@RequestBody String importData){
        System.out.println(importData);
        JSONObject jsonObject = JSONObject.parseObject(importData);
        JSONArray excel = jsonObject.getJSONArray("excel");
        List<Asset> assetList = new LinkedList<>();

        try{
            for (Object excelAsset: excel) {
                //从返回的json中取值并转换
                Map entity = (Map)excelAsset;
                String id=entity.get("资产编号").toString();
                String assetId=entity.get("资产编号").toString();
                String assetName=entity.get("资产名称").toString();
                String supplier=entity.get("供应商").toString();
                String life=entity.get("有效期至").toString();

                String classes=entity.get("资产种类").toString();
                AssetClasses assetClasses = null;
                if(Objects.equals(AssetClasses.HARDWARE_ASSET.getValue(), classes)){
                    assetClasses=AssetClasses.HARDWARE_ASSET;
                }else if(Objects.equals(AssetClasses.SOFTWARE_ASSET.getValue(), classes)){
                    assetClasses=AssetClasses.SOFTWARE_ASSET;
                }else if(Objects.equals(AssetClasses.VIRTUAL_ASSET.getValue(), classes)){
                    assetClasses=AssetClasses.VIRTUAL_ASSET;
                }

                //将转换后的值存入assetList对象数组
                Asset asset = new Asset();
                asset.setId(Long.parseLong(id));
                asset.setAssetId(assetId);
                asset.setAssetName(assetName);
                asset.setSupplier(supplier);
                asset.setStatus(AssetStatus.UNASSIGNED);
                asset.setClasses(assetClasses);
                asset.setLife(SqlDate.StringToDate(life));
                assetList.add(asset);
                //System.out.println(asset);
            }

            assetService.saveBatch(assetList);
        }catch (Exception e){
            return new ResponseResult<>(400,"您的格式有误，请检查资产信息格式");
        }

        return new ResponseResult<>(0,"导入成功!");
    }

    // ----------------------------------------------------------------
    // 资产入库

    @ApiOperation("资产管理员查询待分库资产")
    @PostMapping("/getUnassignedAsset")
    public ResponseResult<List<Asset>> getUnassignedAsset() {
        QueryWrapper<Asset> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Asset::getStatus, AssetStatus.UNASSIGNED);
        List<Asset> list = assetService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "没有待分库的资产");
        }
        return new ResponseResult<>(200, "待分库资产", list);
    }

    @ApiOperation("资产管理员查询对应资产可入库的仓库列表")
    @PostMapping("/getEnableImportRepositories")
    public ResponseResult<List<Repository>> getEnableImportRepositories(@RequestParam("assetId") Long assetId) {
        // 1. 检查资产id对应的资产是否存在，如果资产不存在则返回错误
        if (Objects.isNull(assetService.getById(assetId))) {
            return new ResponseResult<>(400, "资产不存在");
        }
        // 2. 查询资产和仓库对应表，获取资产对应的仓库id
        QueryWrapper<RepositoryAsset> raWrapper = new QueryWrapper<>();
        raWrapper.lambda().eq(RepositoryAsset::getAssetId, assetId);
        RepositoryAsset ra = repositoryAssetService.getOne(raWrapper);


        // 3. 获取除 对应仓库id之外的其他仓库列表
        QueryWrapper<Repository> rWrapper = new QueryWrapper<>();
        if (!Objects.isNull(ra)) {
            Long repositoryId = ra.getRepositoryId();
            rWrapper.lambda().notIn(Repository::getId, repositoryId);
        }
        List<Repository> repositories = repositoryService.list(rWrapper);

        // 4. 判断是否有仓库可存
        if (repositories.isEmpty()) {
            return new ResponseResult<>(400, "无可存放资产仓库");
        }
        return new ResponseResult<>(200, "返回对应资产的可入库仓库", repositories);
    }

    @ApiOperation("资产管理员创建资产入库申请")
    @PostMapping("/insertAssetImport")
    public ResponseResult<List<Asset>> insertAssetImport(@RequestParam("assetId") Long assetId,
                                                         @RequestParam("assetManagerId") Long assetManagerId,
                                                         @RequestParam("repositoryId") Long repositoryId) {
        Asset asset = assetService.getById(assetId);
        // 1. 判断资产是否存在
        if (Objects.isNull(asset)) {
            return new ResponseResult<>(400, "资产不存在");
        }
        // 2. 修改资产状态，如果资产状态为上锁则返回错误，如果为其他状态则修改状态
        if (asset.getStatus() == AssetStatus.LOCK) {
            return new ResponseResult<>(400, "资产状态为上锁");
        }
        asset.setStatus(AssetStatus.LOCK);
        // 3. 判断资产是否已入库
        QueryWrapper<AssetImport> aiWrapper = new QueryWrapper<>();
        aiWrapper.lambda().eq(AssetImport::getAssetId, assetId)
                .eq(AssetImport::getResult, AssetImportResult.CREATE)
                .eq(AssetImport::getIsComplete, IsComplete.UNFINISHED);
        if (!Objects.isNull(assetImportService.getOne(aiWrapper))) {
            return new ResponseResult<>(400, "资产已分库");
        }
        // 4. 判断仓库是否存在
        if (Objects.isNull(repositoryService.getById(repositoryId))) {
            return new ResponseResult<>(400, "仓库不存在");
        }
        // 5. 判断资产是否重复入库
        QueryWrapper<RepositoryAsset> raWrapper = new QueryWrapper<>();
        raWrapper.lambda().eq(RepositoryAsset::getAssetId, assetId);
        RepositoryAsset ra = repositoryAssetService.getOne(raWrapper);
        if (!Objects.isNull(ra)) {
            if (repositoryId.equals(ra.getRepositoryId())) {
                return new ResponseResult<>(400, "资产重复入库");
            }
        }

        // 6. 创建资产入库
        AssetImport assetImport = AssetImport.builder()
                .assetId(assetId)
                .repositoryId(repositoryId)
                .assetManagerId(assetManagerId)
                .result(AssetImportResult.CREATE)
                .isComplete(IsComplete.UNFINISHED)
                .build();
        if (assetImportService.save(assetImport) && assetService.updateById(asset)) {
            // 7. 返回未分库资产列表
            return new ResponseResult<>(200, "创建入库申请成功", getUnassignedAsset().getData());
        }
        return new ResponseResult<>(400, "创建入库申请失败", getUnassignedAsset().getData());
    }

    @ApiOperation("资产管理员获取资产入库申请")
    @PostMapping("/amGetAssetImport")
    public ResponseResult<List<AssetImport>> amGetAssetImport(@RequestParam("assetManagerId") Long assetManagerId) {
        QueryWrapper<AssetImport> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetImport::getAssetManagerId, assetManagerId);
        List<AssetImport> list = assetImportService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无负责的资产入库申请");
        }
        return new ResponseResult<>(200, "资产入库申请", list);
    }

    // ----------------------------------------------------------------
    // 借调审批

    @ApiOperation("资产管理员获取资产借调申请")
    @PostMapping("/amGetAssetSecondments")
    public ResponseResult<List<AssetSecondment>> amGetAssetSecondments(@RequestParam("assetManagerId") Long assetManagerId) {
        QueryWrapper<AssetSecondment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetSecondment::getIsComplete, IsComplete.UNFINISHED)
                .in(AssetSecondment::getResult, AssetSecondmentResult.OPERATIONS_AGREE
                        , AssetSecondmentResult.ASSET_MANAGER_IN_APPROVAL);
        List<AssetSecondment> assetSecondments = asService.list(wrapper);
        if (assetSecondments.isEmpty()) {
            return new ResponseResult<>(400, "借调申请为空");
        }
        return new ResponseResult<>(200, "资产借调申请", assetSecondments);
    }

    @ApiOperation("资产管理员获取可分配资产")
    @PostMapping("/getUnLockAssets")
    public ResponseResult<List<Asset>> getUnLockAssets(@RequestParam("assetSecondmentId") Long assetSecondmentId) {
        AssetSecondment as = asService.getById(assetSecondmentId);
        if (Objects.isNull(as)) {
            return new ResponseResult<>(400, "资产借调申请为空");
        }
        AssetClasses classes = as.getAssetClasses();
        String assetName = as.getAssetName();
        QueryWrapper<Asset> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Asset::getClasses, classes)
                .eq(Asset::getAssetName, assetName)
                .eq(Asset::getStatus, AssetStatus.IN_REPOSITORY);
        List<Asset> assets = assetService.list(wrapper);
        if (assets.isEmpty()) {
            return new ResponseResult<>(400, "没有可分配资产");
        }
        return new ResponseResult<>(200, "可分配资产", assets
        );
    }

    @ApiOperation("资产管理员为借调申请分配资产并创建资产出库")
    @PostMapping("/insertAssetOutbound")
    public ResponseResult<String> insertAssetOutbound(@RequestParam("assetId") Long assetId,
                                                      @RequestParam("assetSecondmentId") Long assetSecondmentId,
                                                      @RequestParam("assetManagerId") Long assetManagerId) {
        // 1. 判断资产是否存在
        Asset asset = assetService.getById(assetId);
        if (Objects.isNull(asset)) {
            return new ResponseResult<>(400, "资产不存在");
        }
        // 2. 判断资产是否未上锁
        if (!Objects.equals(asset.getStatus(), AssetStatus.IN_REPOSITORY)) {
            return new ResponseResult<>(400, "资产不可操作");
        }
        QueryWrapper<AssetOutbound> aoWrapper = new QueryWrapper<>();
        aoWrapper.lambda().eq(AssetOutbound::getAssetSecondmentId, assetSecondmentId)
                .eq(AssetOutbound::getIsComplete, IsComplete.UNFINISHED);
        if (!aoService.list(aoWrapper).isEmpty()) {
            return new ResponseResult<>(400, "已分配资产");
        }
        // 3. 当资产存在且未上锁，则修改资产状态
        UpdateWrapper<Asset> assetUpdateWrapper = new UpdateWrapper<>(asset);
        assetUpdateWrapper.lambda().set(Asset::getStatus, AssetStatus.LOCK)
                .eq(Asset::getStatus, AssetStatus.IN_REPOSITORY);

        // 4. 判断借调申请是否存在
        AssetSecondment as = asService.getById(assetSecondmentId);
        if (Objects.isNull(as)) {
            return new ResponseResult<>(400, "资产借调申请不存在");
        }
        // 5. 判断借调申请是否完成
        if (Objects.equals(as.getIsComplete(), IsComplete.COMPLETE)) {
            return new ResponseResult<>(400, "资产借调申请已完成");
        }

        // 6. 判断借调申请是否完成，判断当前借调结果是否到正确进度
        boolean statusCheck = Objects.equals(as.getResult(), AssetSecondmentResult.OPERATIONS_AGREE)
                || Objects.equals(as.getResult(), AssetSecondmentResult.ASSET_MANAGER_IN_APPROVAL);
        if (!statusCheck) {
            return new ResponseResult<>(400, "资产借调申请进展未到对应步骤");
        }

        // 修改借调申请状态
        UpdateWrapper<AssetSecondment> asWrapper = new UpdateWrapper<>(as);
        asWrapper.lambda().set(AssetSecondment::getResult, AssetSecondmentResult.ASSET_MANAGER_IN_APPROVAL)
                .eq(AssetSecondment::getIsComplete, IsComplete.UNFINISHED);

        // 7. 创建资产出库申请
        QueryWrapper<RepositoryAsset> raWrapper = new QueryWrapper<>();
        raWrapper.lambda().eq(RepositoryAsset::getAssetId, assetId);
        RepositoryAsset ra = raService.getOne(raWrapper);
        if (Objects.isNull(ra)) {
            return new ResponseResult<>(400, "资产不在仓库中，请重新选择");
        }
        Long repositoryId = ra.getRepositoryId();
        AssetOutbound ao = new AssetOutbound();
        ao.setAssetSecondmentId(assetSecondmentId);
        ao.setAssetManagerId(assetManagerId);
        ao.setAssetId(assetId);
        ao.setRepositoryId(repositoryId);
        ao.setResult(AssetOutboundResult.CREATE);
        ao.setIsComplete(IsComplete.UNFINISHED);

        // 修改资产状态
        assetService.update(assetUpdateWrapper);
        // 修改资产借调申请
        asService.update(asWrapper);
        // 创建资产出库申请
        aoService.save(ao);
        return new ResponseResult<>(200, "成功");
    }

    @ApiOperation("资产管理员获取资产出库申请")
    @PostMapping("/amGetAssetOutbounds")
    public ResponseResult<List<AssetOutbound>> amGetAssetOutbounds(@RequestParam("assetManagerId") Long assetManagerId) {
        QueryWrapper<AssetOutbound> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetOutbound::getAssetManagerId, assetManagerId);
        List<AssetOutbound> list = aoService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无负责的出库申请");
        }
        return new ResponseResult<>(200, "获取出库申请", list);
    }

    @ApiOperation("资产管理员更新出库申请")
    @PostMapping("/amUpdateAssetOutbound")
    public ResponseResult<String> amUpdateAssetOutbound(@RequestParam("assetSecondmentId") Long assetSecondmentId,
                                                        @RequestParam("isComplete") IsComplete isComplete) {
        UpdateWrapper<AssetOutbound> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(AssetOutbound::getAssetSecondmentId, assetSecondmentId)
                .set(AssetOutbound::getIsComplete, isComplete);
        if (aoService.update(wrapper)) {
            AssetOutbound assetOutbound = aoService.getById(assetSecondmentId);
            if (assetOutbound.getResult() == AssetOutboundResult.FAILED) {
                aoService.removeById(assetSecondmentId);
            }
            return new ResponseResult<>(200, "更新资产出库申请成功");
        }
        return new ResponseResult<>(400, "资产更新失败");
    }

    @ApiOperation("资产管理员更新资产借调申请")
    @PostMapping("/amUpdateAssetSecondment")
    public ResponseResult<String> amUpdateAssetSecondment(@RequestParam("assetSecondmentId") Long assetSecondmentId,
                                                          @RequestParam("result") AssetSecondmentResult result) {
        // 1. 判断借调申请是否存在
        AssetSecondment assetSecondment = asService.getById(assetSecondmentId);
        if (Objects.isNull(assetSecondment)) {
            return new ResponseResult<>(400, "借调申请不存在");
        }

        // 2. 判断借调申请状态且对应的出库申请是否完成
        if (assetSecondment.getIsComplete() != IsComplete.UNFINISHED
                || assetSecondment.getResult() != AssetSecondmentResult.ASSET_MANAGER_IN_APPROVAL) {
            return new ResponseResult<>(400, "借调申请状态错误");
        }

        // 3. 判断对应借调申请是否存在出库申请
        QueryWrapper<AssetOutbound> aoWrapper = new QueryWrapper<>();
        aoWrapper.lambda().eq(AssetOutbound::getAssetSecondmentId, assetSecondmentId)
                .eq(AssetOutbound::getIsComplete, IsComplete.UNFINISHED);
        if (Objects.nonNull(aoService.getOne(aoWrapper))) {
            return new ResponseResult<>(400, "借调出库正在进行");
        }

        // 4. 资产借调更新
        UpdateWrapper<AssetSecondment> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(AssetSecondment::getId, assetSecondmentId)
                .eq(AssetSecondment::getIsComplete, IsComplete.UNFINISHED)
                .eq(AssetSecondment::getResult, AssetSecondmentResult.ASSET_MANAGER_IN_APPROVAL)
                .set(AssetSecondment::getResult, result)
                .set(AssetSecondment::getIsComplete, IsComplete.COMPLETE);
        if (asService.update(wrapper)) {
            return new ResponseResult<>(200, "资产修改成功");
        }
        return new ResponseResult<>(400, "资产审批错误");
    }

    //-------------------------------------------------------------------------
    // 资产维修

    @Autowired
    private JudgeUtils judgeUtils;

    @ApiOperation("资管获取资产维修申请")
    @PostMapping("amGetAssetMaintenance")
    public ResponseResult<List<AssetMaintenance>> amGetAssetMaintenance(@RequestParam("assetManagerId") Long assetManagerId) {
        List<AssetMaintenance> ams = new ArrayList<>();
        // 1. 获取对应借调的维修申请
        QueryWrapper<AssetAllocation> aaWrapper = new QueryWrapper<>();
        aaWrapper.lambda().eq(AssetAllocation::getAssetManagerId, assetManagerId);
        List<AssetAllocation> aas = aaService.list(aaWrapper);
        if (!aas.isEmpty()) {
            List<Long> assetIds = new ArrayList<>(aas.size());
            for (AssetAllocation aa : aas) {
                assetIds.add(aa.getAssetId());
            }
            // 找到负责借调的审批装维为运维失败的未完成的维修申请
            QueryWrapper<AssetMaintenance> amWrapper = new QueryWrapper<>();
            amWrapper.lambda().in(AssetMaintenance::getAssetId, assetIds)
                    .eq(AssetMaintenance::getStatus, AssetMaintenanceStatus.SECONDMENT)
                    .eq(AssetMaintenance::getIsComplete, IsComplete.UNFINISHED)
                    .eq(AssetMaintenance::getResult, AssetMaintenanceResult.OPERATIONS_FAILED);
            ams.addAll(amService.list(amWrapper));
        }
        // 2. 获取在库的维修申请
        QueryWrapper<AssetMaintenance> amWrapper = new QueryWrapper<>();
        amWrapper.lambda().eq(AssetMaintenance::getStatus, AssetMaintenanceStatus.IN_REPOSITORY)
                .eq(AssetMaintenance::getResult, AssetMaintenanceResult.OPERATIONS_FAILED)
                .eq(AssetMaintenance::getIsComplete, IsComplete.UNFINISHED);
        ams.addAll(amService.list(amWrapper));

        // 3. 判断维修申请是否为空
        if (ams.isEmpty()) {
            return new ResponseResult<>(400, "维修申请为空");
        }
        return new ResponseResult<>(200, "维修申请", ams);
    }

    @ApiOperation("资管审批维修申请")
    @PostMapping("amUpdateAssetMaintenance")
    public ResponseResult<Boolean> amUpdateAssetMaintenance(@RequestParam("assetMaintenanceId") Long assetMaintenanceId,
                                                            @RequestParam("result") AssetMaintenanceResult result,
                                                            @RequestParam("assetManagerId") Long assetManagerId) {
        // 1. 判断申请是否存在
        AssetMaintenance am = amService.getById(assetMaintenanceId);
        if (Objects.isNull(am)) {
            return new ResponseResult<>(400, "维修申请不存在", false);
        }
        // 2. 判断申请是否完成
        if (Objects.equals(am.getIsComplete(), IsComplete.COMPLETE)) {
            return new ResponseResult<>(400, "维修申请已完成", false);
        }
        // 3. 判断申请状态是否符合
        if (!Objects.equals(am.getResult(), AssetMaintenanceResult.OPERATIONS_FAILED)) {
            return new ResponseResult<>(400, "维修申请状态异常", false);
        }
        // 4. 判断资管是否有资格审批
        if (!judgeUtils.checkApprove(am.getAssetId(), assetManagerId)) {
            return new ResponseResult<>(400, "无审批资格", false);
        }
        am.setResult(result);
        am.setIsComplete(IsComplete.COMPLETE);
        if (amService.saveOrUpdate(am)) {
            return new ResponseResult<>(200, "维修审批成功", true);
        }
        return new ResponseResult<>(400, "维修审批失败", false);
    }

    @ApiOperation("资管根据资产id获取资产信息")
    @PostMapping("getAssetByAssetId")
    public ResponseResult<Asset> getAssetByAssetId(@RequestParam("assetId") Long assetId) {
        Asset asset = assetService.getById(assetId);
        if (Objects.isNull(asset)) {
            return new ResponseResult<>(400, "资产不存在");
        }
        return new ResponseResult<>(200, "资产信息", asset);
    }

    //-------------------------------------------------------------------------
    // 资产报废

    @ApiOperation("资产管理员获取资产报废申请")
    @PostMapping("/amGetAssetRetirement")
    public ResponseResult<List<AssetRetirement>> amGetAssetRetirement(@RequestParam("assetManagerId") Long assetManagerId) {
        List<AssetRetirement> ars = new ArrayList<>();
        // 1. 获取对应借调的维修申请
        QueryWrapper<AssetAllocation> aaWrapper = new QueryWrapper<>();
        aaWrapper.lambda().eq(AssetAllocation::getAssetManagerId, assetManagerId);
        List<AssetAllocation> aas = aaService.list(aaWrapper);
        if (!aas.isEmpty()) {
            List<Long> assetIds = new ArrayList<>(aas.size());
            for (AssetAllocation aa : aas) {
                assetIds.add(aa.getAssetId());
            }
            // 找到负责借调的审批装维为运维失败的未完成的维修申请
            QueryWrapper<AssetRetirement> arWrapper = new QueryWrapper<>();
            arWrapper.lambda().in(AssetRetirement::getAssetId, assetIds)
                    .eq(AssetRetirement::getStatus, AssetRetirementStatus.SECONDMENT)
                    .eq(AssetRetirement::getIsComplete, IsComplete.UNFINISHED)
                    .eq(AssetRetirement::getResult, AssetRetirementResult.OPERATIONS_AGREE);
            ars.addAll(arService.list(arWrapper));
        }
        // 2. 获取在库的维修申请
        QueryWrapper<AssetRetirement> arWrapper = new QueryWrapper<>();
        arWrapper.lambda().eq(AssetRetirement::getStatus, AssetRetirementStatus.IN_REPOSITORY)
                .eq(AssetRetirement::getResult, AssetRetirementResult.OPERATIONS_AGREE)
                .eq(AssetRetirement::getIsComplete, IsComplete.UNFINISHED);
        ars.addAll(arService.list(arWrapper));

        // 3. 判断维修申请是否为空
        if (ars.isEmpty()) {
            return new ResponseResult<>(400, "维修申请为空");
        }
        return new ResponseResult<>(200, "维修申请", ars);
    }


    @ApiOperation("资产管理员审批报废申请")
    @PostMapping("/amUpdateAssetRetirement")
    public ResponseResult<Boolean> amUpdateAssetRetirement(@RequestParam("assetRetirementId") Long assetRetirementId,
                                                           @RequestParam("result") AssetRetirementResult result,
                                                           @RequestParam("assetManagerId") Long assetManagerId) {
        // 1. 判断申请是否存在
        AssetRetirement ar = arService.getById(assetRetirementId);
        if (Objects.isNull(ar)) {
            return new ResponseResult<>(400, "报废申请不存在", false);
        }
        // 2. 判断申请是否完成
        if (Objects.equals(ar.getIsComplete(), IsComplete.COMPLETE)) {
            return new ResponseResult<>(400, "报废申请已完成", false);
        }
        // 3. 判断申请状态是否符合
        if (!Objects.equals(ar.getResult(), AssetRetirementResult.OPERATIONS_AGREE)) {
            return new ResponseResult<>(400, "报废申请状态异常", false);
        }
        // 4. 判断资管是否有资格审批
        if (!judgeUtils.checkApprove(ar.getAssetId(), assetManagerId)) {
            return new ResponseResult<>(400, "无审批资格", false);
        }
        ar.setResult(result);
        ar.setIsComplete(IsComplete.COMPLETE);
        if (arService.saveOrUpdate(ar)) {
            return new ResponseResult<>(200, "报废审批成功", true);
        }
        return new ResponseResult<>(400, "报废审批失败", false);
    }

    //资产盘点------------------------------------------------------
    @ApiOperation("资产管理员查询可盘点资产")
    @PostMapping("/getInventoryAsset")
    public ResponseResult<List<Asset>> getInventoryAsset() {
        QueryWrapper<Asset> wrapper = new QueryWrapper<>();
        wrapper.lambda().ne(Asset::getStatus, AssetStatus.UNASSIGNED);
        List<Asset> list = assetService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "没有可盘点的资产");
        }
        return new ResponseResult<>(200, "待盘点资产", list);
    }
}
